<?php


namespace app\index\model;


use think\facade\Cache;
use think\Model;

class UsersModel extends Model
{
    protected $name = 'admin_users';

    protected $autoWriteTimestamp = true;

    /**
     * 获取缓存键
     * @param $keys
     * @return string
     */
    public function cacheKey($keys)
    {
        return md5($keys.Cache::get($this->name,'cache_Virtual'));
    }

    /**
     * 更新缓存键
     * @return bool
     */
    public function updateCacheKey()
    {
        return Cache::set($this->name,'cache_Virtual',time());
    }


    /**
     * 获取用户信息
     * @param $where
     * @return array|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserInfo($where)
    {
        $info = self::where($where)->whereNotIn('associate', [-1, -2])->field('id,openid,pub_openid,unionid,nickname,status,state,headimg')->find();
        $info = $info ? $info->toArray() : [];
        return $info;
    }

    /**
     * 获取虚拟用户
     * @return array|mixed|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getVirtual()
    {
        $cacheKey = $this->cacheKey('getVirtual');
        $all = Cache::get($cacheKey);
        if (!$all) {
            $all = self::where('is_virtual',1)->select();
            $all = $all ? $all->toArray() : [];
            if ($all) {
                Cache::set($cacheKey, $all, 7200);
            }
        }
        return $all;
    }
}